package com.qiche.controller.mgr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.StringUtils;
import com.qiche.aspect.Result;
import com.qiche.constant.AccountType;
import com.qiche.model.CardSubsidy;
import com.qiche.model.Merchant;
import com.qiche.model.User;
import com.qiche.model.WalletLog;
import com.qiche.requestpram.EnchashmentParam;
import com.qiche.service.CardSubsidyService;
import com.qiche.service.EnchashmentService;
import com.qiche.service.MerchantService;
import com.qiche.service.WalletLogService;
import com.qiche.util.UserUtils;

import tk.mybatis.mapper.entity.Example;

/**
 * @author LB
 *
 * @date 2018年5月22日
 */
@RestController
@RequestMapping("/enchashment")
public class EnchashmentController {

	@Autowired
	private EnchashmentService enchashmentService;
	
	@Autowired
	private MerchantService merchantService;

	@Autowired
    private CardSubsidyService cardSubsidyService;
	
	@Autowired
	private WalletLogService walletLogService;
	/**
	 * 核销补贴列表
	 * @return
	 */
	@GetMapping("/cardSubsidyList")
	public Map<String, Object> cardSubsidyList( @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "10") int limit) throws Exception {
		User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
		Merchant merchant= merchantService.selectById(currentUser.getMerchantId().longValue());
		List<CardSubsidy>cardSubsidy;
		Map<String, Object> map = new HashMap<>();
		PageInfo<CardSubsidy> cardSubsidyPageInfo;
		PageHelper.startPage(page, limit);
		if(AccountType.ADMIN == UserUtils.getCurrentUser().getAccountType()){
			cardSubsidy = cardSubsidyService.selectByExample(null);
			cardSubsidyPageInfo= new PageInfo<>(cardSubsidy);
		}else{
		Example example = new Example(Merchant.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("merchantId",merchant.getPiccMid());
		cardSubsidy = cardSubsidyService.selectByExample(example);
		cardSubsidyPageInfo= new PageInfo<>(cardSubsidy);
		}
		map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", cardSubsidyPageInfo.getTotal());
        map.put("data", cardSubsidy);
		return map;
	}
	
	
	/**
	 * 申请提现列表
	 * @param 
	 * @return
	 */
	@GetMapping("/applyEnchashmentList")
	public Map<String, Object> applyEnchashmentList(@RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "10") int limit) throws Exception {
		User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
		PageInfo<WalletLog> cardSubsidyPageInfo;
		Map<String, Object> map = new HashMap<>();
		List<WalletLog> walletLog;
		PageHelper.startPage(page, limit);
		if(AccountType.ADMIN == UserUtils.getCurrentUser().getAccountType()){
		walletLog= walletLogService.selectByExample(null);
		cardSubsidyPageInfo = new PageInfo<>(walletLog);
		}else{
		Example example = new Example(WalletLog.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("merchantId",currentUser.getMerchantId());
		walletLog= walletLogService.selectByExample(example);
		cardSubsidyPageInfo = new PageInfo<>(walletLog);
		}
		map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", cardSubsidyPageInfo.getTotal());
        map.put("data", walletLog);
		return map;
	}
	
	
	/**
	 * 申请提现
	 * @param enchashmentParam
	 * @return
	 */
	@PostMapping("/applyEnchashment")
	public Result<?> applyEnchashment(@RequestBody EnchashmentParam enchashmentParam) throws Exception {
		Result<?> result = enchashmentService.enchashment(enchashmentParam);
		return result;
	}
	
	/**
	 * 审核提现
	 * @param 
	 * @return
	 */
	@PostMapping("/auditTransfer")
	public Result<?> AuditTransfer(@RequestParam String id,@RequestParam String merchantId,@RequestParam Integer status) throws Exception{
		Result<?> result = enchashmentService.AuditTransfer(id, merchantId,status);
		return result;
	}

}
